package org.durandj.ystream.client.ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.security.InvalidParameterException;

import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 * A dialog for connecting to a YStream Server with the client.
 * 
 * @author durandj
 * @version 1.0
 *
 */
public class ServerConnectDialog extends javax.swing.JDialog
{
	{
		//Set Look & Feel
		try
		{
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	private static final long serialVersionUID = 3989825569549121938L;
	
	/**
	 * The user hit the connect button.
	 */
	public static final int RESULT_CONNECT = 0;
	
	/**
	 * The user hit the cancel button.
	 */
	public static final int RESULT_CANCEL = 1;
	
	private JLabel lblServerAddress;
	private JButton btnCancel;
	private JButton btnConnect;
	private JTextField txtServerAddress;
	private JLabel lblPort;
	private JTextField txtPort;
	
	/**
	 * The result of the window closing.
	 */
	private int result;

	/**
	 * The address of the server.
	 */
	private String serverAddress;
	
	/**
	 * The port of the server.
	 */
	private int port;

	/**
	 * Creates a server connection dialog.
	 * 
	 * @param frame - the parent window
	 */
	public ServerConnectDialog(JFrame frame)
	{
		super(frame);
		initGUI();
		
		result = -1;
	}
	
	private void initGUI()
	{
		try
		{
			{
				this.setTitle("Connect to Server");
				this.setResizable(false);
				getContentPane().setLayout(null);
				this.setModal(true);
				{
					lblServerAddress = new JLabel();
					getContentPane().add(lblServerAddress);
					lblServerAddress.setText("Server address:");
					lblServerAddress.setBounds(6, 12, 87, 16);
				}
				{
					txtServerAddress = new JTextField();
					getContentPane().add(txtServerAddress);
					txtServerAddress.setBounds(99, 6, 289, 28);
					
					txtServerAddress.setToolTipText("The address of the YStream server you want to connect to.");
				}
				{
					lblPort = new JLabel();
					getContentPane().add(lblPort);
					lblPort.setText("Port:");
					lblPort.setBounds(6, 46, 25, 16);
				}
				{
					txtPort = new JTextField();
					getContentPane().add(txtPort);
					txtPort.setBounds(99, 40, 82, 28);
					
					txtPort.setToolTipText("The port you want to use to connect to the YStream Server.");
				}
				{
					btnConnect = new JButton();
					getContentPane().add(btnConnect);
					btnConnect.setText("Connect");
					btnConnect.setBounds(241, 68, 74, 28);
					
					btnConnect.setToolTipText("Connect to the specified server.");
					
					btnConnect.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0)
						{
							if(txtServerAddress.getText().isEmpty() ||
							   txtPort.getText().isEmpty())
							{
								JOptionPane.showMessageDialog(ServerConnectDialog.this,
								                              "Must give a server address and port number.",
								                              "Invalid server",
								                              JOptionPane.ERROR_MESSAGE);
								return;
							}
							
							try
							{
								serverAddress = txtServerAddress.getText();
								port = Integer.parseInt(txtPort.getText());
							}
							catch(NumberFormatException e)
							{
								JOptionPane.showMessageDialog(ServerConnectDialog.this,
								                              "Must give a valid port number.",
								                              "Invalid server port",
								                              JOptionPane.ERROR_MESSAGE);
								return;
							}
							
							result = RESULT_CONNECT;
							setVisible(false);
						}
					});
				}
				{
					btnCancel = new JButton();
					getContentPane().add(btnCancel);
					btnCancel.setText("Cancel");
					btnCancel.setBounds(321, 68, 67, 28);
					
					btnCancel.setToolTipText("Cancel the connection.");
					
					btnCancel.addActionListener(new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent arg0)
						{
							result = RESULT_CANCEL;
							setVisible(false);
						}
					});
				}
			}
			this.setSize(400, 140);
			this.setLocationRelativeTo(this.getParent());
			
			// We need to know if the user has hit the exit button so let's listen for it.
			addWindowListener(new WindowAdapter() {
				@Override
				public void windowClosing(WindowEvent e)
				{
					result = RESULT_CANCEL;
				}
			});
			
			getRootPane().setDefaultButton(btnConnect);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * Gets the result of the window closing.
	 * 
	 * @return the result
	 */
	public int getResult()
	{
		return result;
	}
	
	/**
	 * Gets the selected server address.
	 * 
	 * @return the server address
	 */
	public String getServerAddress()
	{
		return serverAddress;
	}
	
	/**
	 * Sets the server address.
	 * 
	 * @param serverAddress - the server address
	 */
	public void setServerAddress(String serverAddress)
	{
		if(serverAddress == null || serverAddress.isEmpty())
			throw new InvalidParameterException("Must give a valid server address.");
		
		this.serverAddress = serverAddress;
		txtServerAddress.setText(serverAddress);
	}
	
	/**
	 * Gets the selected server port.
	 * 
	 * @return the port
	 */
	public int getServerPort()
	{
		return port;
	}
	
	/**
	 * Sets the server port.
	 * 
	 * @param port - the server port
	 */
	public void setServerPort(int port)
	{
		if(port <= 0)
			throw new InvalidParameterException("Must give a valid server port.");
		
		this.port = port;
		txtPort.setText(Integer.toString(port));
	}
}
